<?php
defined('SYSPATH') or die('Access error!');
/**
 * 操作供求的类
 * ============================================================================
 * 功能描述： 
 * ============================================================================
 * $Author: liuxudong $
 */
class Controller_Trade extends Controller_Trade_Main
{

    private $trade;

    public function before()
    {
        parent::before();
        $this->trade = Model::factory('trade_post');
        I18n::set_current_file('trade');
    }

    //供求首页
    public function action_index()
    {
        //当前位置
        $this->set_define_position();
        //获取供求相关分类
        $trade_category = $this->get_date_as_array(SYSTEM_TRADE_CATEGORY, 'trade');
        $this->template->assign('category_list', $trade_category);
        $this->template->view('trade_index');
    }

    //任务列表
    public function action_supply()
    {
        $this->display_list($this->handle_common('supply'));
    }

    //需求列表
    public function action_demand()
    {
        $this->display_list($this->handle_common('demand'));
    }

    public function action_list()
    {
        $id = intval($this->request->param('id'));
        if (empty($id))
        {
            $this->request->redirect('trade/index');
        }

        $this->display_list($this->handle_common('list'));
    }

    public function handle_common($type)
    {
        $data = array();
        $id = intval($this->request->param('id'));
        if (!empty($id))
        {
            //获取分类的所有子分类
            $childs = $this->get_category_childs($id, 'trade');
            $data['category_id'] = !empty($childs) ? $childs : $id;
            //获取当前位置
            $this->get_category_position($id, 'trade', 'trade/' . $type);
            //设置当前分类的seo资料
            $this->get_category_seo($id, 'trade');
            $this->template->assign('selected_category', $id);
        }
        else
        {
            $this->set_define_position(__($type . '_list'));
            $this->set_define_seo(__($type . '_list'));
            $this->template->assign('selected_category', SYSTEM_TRADE_CATEGORY);
        }
        $data['post_type'] = $type;
        return $data;
    }

    public function display_list($data = array())
    {
        //获取供求相关分类
        $trade_category = $this->get_date_as_array(SYSTEM_TRADE_CATEGORY, 'trade');
        $this->template->assign('url_type', $data['post_type']);
        $this->template->assign('category_list', $trade_category);
        $data = !empty($data) ? $data : '';
        if (isset($data['post_type']) && $data['post_type'] == 'list')
        {
            unset($data['post_type']);
        }
        $trade_list = $this->trade->get_trade_list($data);
        $this->template->assign('trade_list', $trade_list['list']);
        $this->template->assign('page', $trade_list['page']);

        $page_num = isset($_GET['page']) ? intval($_GET['page']) : 1;
        $this->template->assign('page_num', $page_num);
        $this->template->assign('server_time', Date::get_time());
        $this->template->view('trade_list');
    }

    //详细页面
    public function action_detail()
    {
        $id = intval($this->request->param('id'));
        if (empty($id))
        {
            $this->show_404();
        }
        $detail = $this->trade->get_trade_detail($id);

        if (empty($detail))
        {
            $this->request->redirect('trade/index');
        }
        //当前位置
        $this->get_category_position($detail['category_id'], 'trade', $this->trade->create_trade_list_url());
        //详细供求的seo资料
        $this->detail_seo($detail['category_id'],'trade', $detail['name'], $detail['meta_keywords'], $detail['meta_description']);

        //引入地区缓存分类
        $area_arr = $this->get_cache_file('area');
        if (!empty($detail['province_id']))
        {
            $detail['province'] = isset($area_arr[$detail['province_id']]) ? $area_arr[$detail['province_id']]['name'] : '';
            if (!empty($detail['city_id']))
            {
                $detail['city'] = isset($area_arr[$detail['city_id']]) ? $area_arr[$detail['city_id']]['name'] : '';
            }
        }

        //获取已经留言
        $comments = Model::factory('trade_comments')->get_comments_list(array('trade_id' => $id));
        $this->template->assign('comments', $comments);
        $this->template->assign('data', $detail);
        $this->template->assign('server_time', Date::get_time());
        $this->template->view('trade_detail');
    }

    //任务留言
    public function action_comments()
    {
        if (!$this->logged_in())
        {
            $this->request->redirect('user/login');
        }

        if ($_POST)
        {
            $data = array();
            $data['trade_id'] = intval(Arr::get($_POST, 'trade_id', ''));
            $trade_detail = $this->trade->get_row(array('id' => $data['trade_id']), 'comment_count,name');
            if (empty($data['trade_id']) || empty($trade_detail))
            {
                $this->show_404();
            }

            $this->is_allowed('trade/detail/' . $data['trade_id']);

            if (!$this->valid_post($_POST))
            {
                $this->request->redirect('trade/detail/' . $data['trade_id']);
            }
            $data['content'] = HTML::get_safe_content(Arr::get($_POST, 'content', ''));
            $data['user_id'] = $this->user_detail['id'];
            $data['username'] = $this->user_detail['username'];
            $data['email'] = $this->user_detail['email'];
            $data['add_time'] = Date::get_time();

            if (Model::factory('trade_comments')->insert($data))
            {
                $this->trade->update(array('comment_count' => $trade_detail['comment_count'] + 1), array('id' => $data['trade_id']));
                Message::success('留言成功');
                $this->request->redirect('trade/detail/' . $data['trade_id']);
            }
        }
        $this->request->redirect('trade/index');
    }

    //提交任务
    public function action_add()
    {
        if (!$this->logged_in())
        {
            $this->request->redirect('user/login');
        }

        $this->is_allowed('trade/index');
        //当前的位置
        $this->set_define_position(__('free add info'));
        //seo相关
        $this->set_define_seo(__('free add info'));
        if ($_POST)
        {
            if (!$this->valid_post($_POST))
            {
                $this->request->redirect('trade/supply');
            }
            $data = array();
            $data['content'] = HTML::get_safe_content(Arr::get($_POST, 'content', ''));
            $data['name'] = HTML::get_safe_content(Arr::get($_POST, 'name', ''));
            $data['post_type'] = HTML::get_safe_content(Arr::get($_POST, 'post_type', ''));
            $data['mobile_phone'] = HTML::get_safe_content(Arr::get($_POST, 'mobile_phone', ''));
            $data['telphone'] = HTML::get_safe_content(Arr::get($_POST, 'telphone', ''));
            $data['limit_time'] = Date::get_mk_time(HTML::get_safe_content(Arr::get($_POST, 'limit_time', '')));
            $data['category_id'] = Arr::get($_POST, 'category_id', '');
            $data['account'] = HTML::get_safe_content(Arr::get($_POST, 'account', ''));
            $data['user_id'] = $this->user_detail['id'];
            $data['province_id'] = Arr::get($_POST, 'province_id', 0);
            $data['city_id'] = Arr::get($_POST, 'city_id', 0);
            $data['address'] = HTML::get_safe_content(Arr::get($_POST, 'address', ''));
            $data['notify_email'] = $this->user_detail['email'];
            $data['post_type'] = Arr::get($_POST, 'post_type', 'supply');
            $data['view_count'] = 1;
            $data['latest_activity'] = Date::get_time();
            $data['add_time'] = Date::get_time();
            $data['update_time'] = Date::get_time();
            $data['meta_title'] = $data['name'];
            $data['meta_keywords'] = SplitWord::instance()->get_keyword(SplitWord::instance()->split_result($data['name']));
            SplitWord::instance()->Clear();
            $data['meta_description'] = Text::limit_chars($data['content'], 100);

            if (!$this->trade->validate($data))
            {
                Message::error(__('validate error'));
                $this->request->redirect('trade/supply');
            }
            $post_id = $this->trade->insert($data);
            if (!empty($post_id))
            {
                //处理相关的附件和图片
                if (isset($_FILES) && $_FILES['attachment']['tmp_name'] != '')
                {
                    $time_path = File::create_time_path();
                    $file_detail = $this->save_simple_detail(SHOP_TRADE_IMAGES_SAVE_PATH . $this->user_detail['id'] . '/' . $time_path, 'attachment');
                    if (Arr::validate_arr($file_detail))
                    {
                        foreach ($file_detail as $value)
                        {
                            $att_arr = array();
                            $att_arr['module'] = 'trade';
                            $att_arr['file_name'] = $value['file_name'];
                            $att_arr['file_size'] = $value['file_size'];
                            $att_arr['file_ext'] = $value['file_ext'];
                            $att_arr['file_path'] = File::change_path($time_path);
                            $att_arr['file_realname'] = $value['file_realname'];
                            $att_arr['user_id'] = $this->user_detail['id'];
                            $att_arr['add_ip'] = $value['ip'];
                            $att_arr['add_time'] = Date::get_time();
                            $att_arr['is_image'] = $value['is_image'];
                            //添加附件
                            $attachment_id = Model::factory('attachment')->insert($att_arr);
                            if ($attachment_id)
                            {
                                //添加任务和附件的关联关系
                                $this->trade->add_trade_attachment($post_id, $attachment_id);
                            }
                        }
                    }
                }
                Message::success(__('insert trade success'));
                $this->request->redirect('trade/supply');
            }
        }
        //获取供求平台的分类
        $this->template->assign('category_option', $this->get_category_cache_option('trade'));
        //根据用户所在地
        $area = $this->get_area_cache_file();
        $this->template->assign('province', $area['province']);
        $this->template->view('trade_add');
    }

}

